/**
 * 

 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.manager;

import java.io.InputStream;
import java.util.UUID;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.multipart.MultipartFile;

import com.legendshop.oa.constants.Constants;
import com.legendshop.oa.constants.InvokeRetObj;
import com.legendshop.oa.constants.ValidateFileProcessor;
import com.legendshop.oa.tag.ResourcePathUtil;
import com.legendshop.oa.util.FileProcessor;
import com.legendshop.util.AppUtils;

/**
 * 本地文件管理
 *
 */
public class LocalAttachmentManagerImpl extends CommonAttachmentManagerImpl implements InitializingBean {

	private String bigFilesAbsolutePath;

	private String snapshotBigFilesAbsolutePath;

	/**
	 * 文件流形式 上传
	 */
	@Override
	public String upload(InputStream is, String fileName, long fileSize) {
		if (!ValidateFileProcessor.validateFile(fileName, fileSize)) {
			log.warn("file {} format error", fileName);
			return Constants.PHOTO_FORMAT_ERR;
		}
		String extName = FileProcessor.getFileExtName(fileName);
		String lastFileName = UUID.randomUUID().toString() + extName;
		return FileProcessor.uploadFile(is, lastFileName);
	}

	/**
	 * For Spring mvc文件上传
	 */
	@Override
	public String upload(MultipartFile file) {
		if (file == null) {
			log.warn("file is null");
			return Constants.PHOTO_FORMAT_ERR;
		}

		if (!ValidateFileProcessor.validateFile(file.getOriginalFilename(), file.getSize())) {
			log.warn("file {} format error", file.getOriginalFilename());
			return Constants.PHOTO_FORMAT_ERR;
		}
		// 保存图片到本地磁盘
		String result = FileProcessor.uploadFileAndCallback(file);
		if (AppUtils.isBlank(result)) {
			log.warn("上传文件失败 file name is {}", file.getName());
		}
		return result;
	}

	/**
	 * 真实删除图片
	 * 
	 * @param fileName
	 *            文件路径
	 * @return 0 成功 -1 删除失败 1 没有文件 2 其他异常
	 */
	@Override
	public int deleteTruely(String fileName) {
		if (AppUtils.isBlank(fileName)) {
			return 0;
		}
		// 删除大图
		InvokeRetObj result = FileProcessor.deleteFile(bigFilesAbsolutePath + fileName, false);
		if (result.isFlag()) {
			// 删除图片的缩略图
			for (String sacle : scaleList.keySet()) {
				StringBuilder sb = new StringBuilder(ResourcePathUtil.getSmallFilesAbsolutePath());
				sb.append("/").append(sacle).append("/").append(fileName);
				FileProcessor.deleteFile(sb.toString(), false);
			}
		} else {
			log.warn("delete file failed return code is {}, msg is {} ", result.getRetCode(), result.getMsg());
		}

		return result.getRetCode();
	}

	/**
	 * 系统初始化
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		bigFilesAbsolutePath = ResourcePathUtil.getBigFilesAbsolutePath();
		if (bigFilesAbsolutePath != null && !bigFilesAbsolutePath.endsWith("/")) {
			bigFilesAbsolutePath = bigFilesAbsolutePath + "/";
		}

		snapshotBigFilesAbsolutePath = ResourcePathUtil.getSnapshotBigFilesAbsolutePath();
		if (snapshotBigFilesAbsolutePath != null && snapshotBigFilesAbsolutePath.endsWith("/")) {
			snapshotBigFilesAbsolutePath = snapshotBigFilesAbsolutePath + "/";
		}

	}

}
